package com.wcq.studentbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcq.studentbackend.dto.StudentDetailDTO;
import com.wcq.studentbackend.entity.Student;
import com.wcq.studentbackend.mapper.StudentMapper;
import com.wcq.studentbackend.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student getByStuNo(String stuNo) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getStuNo, stuNo);
        return getOne(queryWrapper);
    }

    @Override
    public List<Student> getByClassId(Integer classId) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getClassId, classId);
        return list(queryWrapper);
    }

    @Override
    public boolean isStuNoExists(String stuNo) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getStuNo, stuNo);
        return count(queryWrapper) > 0;
    }

    @Override
    @Transactional
    public boolean removeByIds(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        return super.removeByIds(ids);
    }

    @Override
    public StudentDetailDTO getStudentDetailByStuNo(String stuNo) {
        if (stuNo == null || stuNo.trim().isEmpty()) {
            log.warn("Attempted to get student detail with null or empty stuNo.");
            return null;
        }
        // 假设 StudentMapper 中有一个方法可以连接查询得到 StudentDetailDTO
        return studentMapper.findDetailByStuNo(stuNo);
    }
}